#include "MenuActions.h"
#include <EventManager.h>
#include "..\Constants.h"

// ---------------------------------------------------
//  Main menu
// ---------------------------------------------------
MainMenuAction::MainMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("MainMenu",materialName,layerName) , m_Core(core) {
		createTextNode("BigText","BigXScale");
		createTextNode("NormalText","XScale");	
		addText("BigText",370,250,"Main Menu");
		addSprite(ds::Vec2(260,100),ds::Rect(350,0,510,60));
		setMouseOffset(0,-10);
		addLink(100,"NormalText",410,340,190,18,"Timer mode",LINK_COLOR);
		addLink(101,"NormalText",380,380,280,18,"Coverage mode",LINK_COLOR);
		addLink(102,"NormalText",450,420,110,18,"Scores",LINK_COLOR);
		addLink(103,"NormalText",445,460,120,18,"Credits",LINK_COLOR,"CreditsMenu");
		addLink(104,"NormalText",425,520,160,18,"Exit Game",LINK_COLOR);
}

void MainMenuAction::onMouseOver( int id ) {
	setLinkColor(id,"NormalText",SELECTED_LINK_COLOR);
	gEngine->getAudioManager().play("MenuSelection",50,false);
}

void MainMenuAction::onMouseOut( int id ) {
	setLinkColor(id,"NormalText",LINK_COLOR);
}

void MainMenuAction::onClicked( int id ) {
	if ( id == 100 ) {
		m_Core->settings().coverageMode = false;
		m_Core->settings().coverage = 0;
		m_Core->settings().level = 1;
		m_Core->settings().timerMode = true;
		m_Core->settings().ttl = 120;
		gEngine->getActionManager().stopAction("MainMenu");
		gEngine->getActionManager().startAction("TimeMenu");
	}
	else if ( id == 101 ) {
		m_Core->settings().coverageMode = true;
		m_Core->settings().coverage = 50;
		m_Core->settings().level = 1;
		m_Core->settings().timerMode = false;
		m_Core->settings().ttl = 0;
		gEngine->getActionManager().stopAction("MainMenu");
		gEngine->getActionManager().startAction("LevelMenu");	
	}
	//else if ( id == 102 ) {
		//gEngine->activateGameState("Scores");
	//}	
	else if ( id == 104 ) {
		gEngine->shutdown();
	}
}
// ---------------------------------------------------
//  Credits menu
// ---------------------------------------------------
CreditsMenuAction::CreditsMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("CreditsMenu",materialName,layerName) , m_Core(core) {
		setNextStateName("MainMenu");
		createTextNode("BigText","BigXScale");
		createTextNode("NormalText","XScale");
		addSprite(ds::Vec2(260,100),ds::Rect(350,0,510,60));
		addText("BigText",400,250,"Credits:");
		addText("NormalText",320,360,"Coding:");
		addText("NormalText",320,390,"Art:");
		addText("NormalText",320,420,"Font:");
		addText("NormalText",320,450,"Sounds:");
		addText("NormalText",320,480,"Music:");
		addText("NormalText",500,360,"Andreas Mecky");
		addText("NormalText",500,390,"Andreas Mecky");
		addText("NormalText",500,420,"XScale by faktor-i.de");
		addText("NormalText",500,450,"www.freesounds.org");
		addText("NormalText",500,480,"Opium");
		addLink(98,"NormalText",220,580,740,18,"Click here to return to main menu",LINK_COLOR,"MainMenu");
		stop();
}

void CreditsMenuAction::onMouseOver( int id ) {
	setLinkColor(id,"NormalText",SELECTED_LINK_COLOR);
	gEngine->getAudioManager().play("MenuSelection",50,false);
}

void CreditsMenuAction::onMouseOut( int id ) {
	setLinkColor(id,"NormalText",LINK_COLOR);
}

// ---------------------------------------------------
//  Level menu
// ---------------------------------------------------
LevelMenuAction::LevelMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("LevelMenu",materialName,layerName) , m_Core(core) {
		createTextNode("BigText","BigXScale");
		createTextNode("NormalText","XScale");
		addText("BigText",370,250,"Select level");
		setMouseOffset(0,-10);
		std::vector<std::string> levelNames;
		core->gutter().getLevelNames(levelNames);
		int yp = 340;
		for ( size_t i = 0; i < levelNames.size(); ++i ) {
			int len = levelNames[i].length() * 20 + 40;
			addLink(120+i,"NormalText",320,yp,len,18,ds::string::to_string(i+1)+". "+levelNames[i],LINK_COLOR);
			yp += 40;
		}
		addLink(99,"NormalText",425,yp+80,160,18,"Back",LINK_COLOR);
		stop();
}

void LevelMenuAction::onMouseOver( int id ) {
	setLinkColor(id,"NormalText",SELECTED_LINK_COLOR);
	gEngine->getAudioManager().play("MenuSelection",50,false);
}

void LevelMenuAction::onMouseOut( int id ) {
	setLinkColor(id,"NormalText",LINK_COLOR);
}

void LevelMenuAction::onClicked( int id ) {
	LOG(logINFO) << "LevelMenuState onClicked id " << id;
	if ( id == 99 ) {
		// if timer mode is selected then we have to enable the time menu
		if ( m_Core->settings().timerMode ) {
			gEngine->getActionManager().stopAction("LevelMenu");
			gEngine->getActionManager().startAction("TimeMenu");
		}		
		else {
			gEngine->getActionManager().stopAction("LevelMenu");
			gEngine->getActionManager().startAction("MainMenu");			
		}
	}
	else {
		int lv = id - 120;
		m_Core->settings().level = lv;
		gEngine->getActionManager().stopAction("LevelMenu");
		FIRE_EVENT(EVN_START_GAME,&m_Core->settings(),sizeof(GameSettings));
	}	
}

// ---------------------------------------------------
//  Time menu
// ---------------------------------------------------
TimeMenuAction::TimeMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("TimeMenu",materialName,layerName) , m_Core(core) {
		createTextNode("BigText","BigXScale");
		createTextNode("NormalText","XScale");
		addText("BigText",370,250,"Select timer");
		setMouseOffset(0,-10);
		addLink(110,"NormalText",420,340,170,18,"2 minutes",LINK_COLOR);
		addLink(111,"NormalText",420,380,170,18,"4 minutes",LINK_COLOR);
		addLink(112,"NormalText",420,420,170,18,"6 minutes",LINK_COLOR);		
		addLink(114,"NormalText",470,520,80,18,"Back",LINK_COLOR);
		stop();
}

void TimeMenuAction::onMouseOver( int id ) {
	setLinkColor(id,"NormalText",SELECTED_LINK_COLOR);
	gEngine->getAudioManager().play("MenuSelection",50,false);
}

void TimeMenuAction::onMouseOut( int id ) {
	setLinkColor(id,"NormalText",LINK_COLOR);
}

void TimeMenuAction::onClicked( int id ) {
	LOG(logINFO) << "TimeMenuState onClicked id " << id;
	if ( id == 110 ) {		
		m_Core->settings().timerMode = true;
		m_Core->settings().ttl = 120;
		gEngine->getActionManager().stopAction("TimeMenu");
		gEngine->getActionManager().startAction("LevelMenu");
	}
	else if ( id == 111 ) {
		m_Core->settings().timerMode = true;
		m_Core->settings().ttl = 240;
		gEngine->getActionManager().stopAction("TimeMenu");
		gEngine->getActionManager().startAction("LevelMenu");
	}
	else if ( id == 112 ) {
		m_Core->settings().timerMode = true;
		m_Core->settings().ttl = 360;
		gEngine->getActionManager().stopAction("TimeMenu");
		gEngine->getActionManager().startAction("LevelMenu");
	}	
	else if ( id == 114 ) {
		gEngine->getActionManager().stopAction("TimeMenu");
		gEngine->getActionManager().startAction("MainMenu");
	}
}

// ---------------------------------------------------
//  GameOverMenuAction
// ---------------------------------------------------
GameOverMenuAction::GameOverMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("GameOverMenu",materialName,layerName) , m_Core(core) {
		setMouseOffset(0,-10);
		createTextNode("BigText","BigXScale");
		createTextNode("NormalText","XScale");
		addText("BigText",370,200,"Game over");
		addText("NormalText",310,330,"Level:");
		addText("NormalText",310,360,"Points:");
		addText("NormalText",310,390,"Coverage:");
		addText("NormalText",310,420,"Time:");
		addText("NormalText",310,450,"Highscore:");	
		m_LevelIdx = addText("NormalText",590,330,"1");
		m_PointsIdx = addText("NormalText",590,360,"012345");
		m_CoverageIdx = addText("NormalText",590,390,"20 %");
		addText("NormalText",590,420,"02:00");	
		addText("NormalText",590,450,"112233");
		addLink(131,"NormalText",360,540,310,18,"Continue to next level",LINK_COLOR);
		addLink(130,"NormalText",360,580,310,18,"Back to main menu",LINK_COLOR);
		stop();
}

void GameOverMenuAction::start() {
	setText("NormalText",m_LevelIdx,590,330,ds::string::to_string(m_Core->score().level));
	setText("NormalText",m_PointsIdx,590,360,ds::string::to_string(m_Core->score().points));
	setText("NormalText",m_CoverageIdx,590,390,ds::string::to_string(m_Core->score().coverage)+" %");
	if ( m_Core->score().gameOver ) {
		setLinkState(131,"NormalText",false);
	}
	else {
		setLinkState(131,"NormalText",true);
	}
	BasicMenuAction::start();
}

void GameOverMenuAction::onMouseOver( int id ) {
	setLinkColor(id,"NormalText",SELECTED_LINK_COLOR);
	gEngine->getAudioManager().play("MenuSelection",50,false);
}

void GameOverMenuAction::onMouseOut( int id ) {
	setLinkColor(id,"NormalText",LINK_COLOR);
}

void GameOverMenuAction::onClicked( int id ) {
	if ( id == 130 ) {		
		gEngine->getActionManager().stopAction("GameOverMenu");
		gEngine->getActionManager().startAction("MainMenu");
	}	
}